package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 禁停区
 *
 * @Author: chen
 * @Description: 禁停区
 * @Date: 创建于 13:28 2020/4/15
 */
@RestController
@RequestMapping("/prohibitArea")
public class ProhibitAreaController {
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private ProhibitAreaService prohibitAreaService;
    @Autowired
    private MachineBorrowService machineBorrowService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private TokenService tokenService;

    /**
     * 获取附近禁停区
     *
     * @param mapType   地图类型
     * @param lo        经度
     * @param la        纬度
     * @param accountId 区域id
     */
    @RequestMapping("/getByArea")
    public Result<List<ProhibitArea>> getByArea(Integer mapType, Double lo, Double la, Integer accountId, String token) {
        /**区域id*/
        Assert.notNull(accountId, "-10001", "区域id不能为空");
        Assert.notNull(lo, "-2001", "经度不能为空");
        Assert.notNull(la, "-2002", "纬度不能为空");

        if (mapType == null) {
            mapType = MapConstant.MAP_BAIDU;
        }

        /*校准百度经纬度为原始经纬度*/
        Point point = new Point(lo, la);
        point = calibrateService.calibrate(point, mapType, SysConstant.ORIGINAL);
        /**如果用户借车了，只展示借车区域禁停区*/
        if (token != null) {
            User user = tokenService.getUser(token);
            if (user != null) {
                MachineBorrow machineBorrow = machineBorrowService.getByUserId(user.getUserId());
                if (machineBorrow != null) {
                    Machine machine = machineService.getByMachineId(machineBorrow.getMachineId());
                    accountId = machine.getAccountId();
                }
            }
        }

        List<ProhibitArea> list = prohibitAreaService.getByArea(accountId, point.getX(), point.getY());
        if (list.size() > 0) {
            calibrateService.celibrateProhibit(list, mapType);
        }
        return Result.success(list);
    }

}
